package org.iteam.xlz.core.service.order;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.order.OrderInfo;

public class OrderInfoService extends BaseService<OrderInfo>{
	
	static OrderInfoService instance;
	
	private OrderInfoService(){
	}
	
	public static OrderInfoService getInstance(){
		if(instance==null){
			instance=new OrderInfoService();
		}
		return instance;
	}
	
	public OrderInfo findOne(String oid){
		try {
			return super.show("select * from orderinfo where oid=?",oid);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean validate(String oid){
		try {
			return null!=super.show("select * from orderinfo where oid=?",oid);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean finishOrder(OrderInfo model){
		try {
			return super.updates("update orderinfo set status=2,updatetime=now() where oid=?",model.getOid())==1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean closeOrder(OrderInfo model){
		Connection con=null;
		try {
			con=super.getConnection(false);
			int i=super.updates(con,"update orderinfo set status=4,updatetime=now() where oid=?",model.getOid());
			int j=0;
			if(model.getOrdertype()==2){
				j=1;
			}else{
				j=super.updates(con,"update product set productamount=productamount+? where pid=?",model.getAmount(),model.getPid());
			}
			if(i==1&&j==1){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			return false;
		}
	}
	
	public long counts(String oid,String ptid,String productname,String orderStatus,String mobile,
			String terminal,String leftCreateTime,String rightCreateTime,String leftPayTime,String rightPayTIme){
		try {
			SqlBuilder sql=SqlBuilder.createQuery("orderinfo l left join product r on l.pid=r.pid left join userinfo u on l.uid=u.uid","count(1)");
			if(StringUtils.isNotBlank(oid)){
				sql.addWhere("l.oid",oid);
			}
			if(StringUtils.isNotBlank(productname)){
				sql.addWhere("r.productname","%"+productname+"%","like");
			}
			if(StringUtils.isNotBlank(orderStatus)){
				sql.addWhere("l.status",orderStatus);
			}
			if(StringUtils.isNotBlank(ptid)){
				sql.addWhere("r.ptid",ptid);
			}			
			if(StringUtils.isNotBlank(mobile)){
				sql.addWhere("u.mobile","%"+mobile+"%","like");
			}
/*			if(StringUtils.isNotBlank(terminal)){
				sql.addWhere("terminal", value);
			}*/
			if(StringUtils.isNotBlank(leftCreateTime)&&StringUtils.isNotBlank(rightCreateTime)){
				sql.addWhere(" l.createtime between '"+leftCreateTime+"' and '"+rightCreateTime+"'");
			}
			if(StringUtils.isNotBlank(leftPayTime)&&StringUtils.isNotBlank(rightPayTIme)){
				sql.addWhere(" l.paytime between '"+leftPayTime+"' and '"+rightPayTIme+"'");
			}
			return super.counts(sql.getSql(),sql.getParams());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<OrderInfo> query(String oid,String ptid,String productname,String orderStatus,String mobile,String terminal,
			String leftCreateTime,String rightCreateTime,String leftPayTime,String rightPayTime,long page,long rows,boolean limit){
		StringBuilder sql=new StringBuilder("select l.oid,u.mobile as uid,pp.productname as pid,l.totalprice,l.amount,l.paytype,l.tradeno,l.status,l.createtime"+
	    ",l.ordertype,l.paytime,l.linkman,l.mobile,l.addr from orderinfo l left join product r on l.pid=r.pid left join userinfo u on l.uid=u.uid left join productputaway pp on l.ppid=pp.ppid where 1=1");
		List<Object> params=new ArrayList<Object>();
		if(StringUtils.isNotBlank(oid)){
			sql.append(" and l.oid=?");
			params.add(oid);
		}
		if(StringUtils.isNotBlank(productname)){
			sql.append(" and r.productname like ?");
			params.add("%'"+productname+"'%");
		}
		if(StringUtils.isNotBlank(orderStatus)){
			sql.append(" and l.status=?");
			params.add(orderStatus);
		}
		if(StringUtils.isNotBlank(ptid)){
			sql.append(" and r.ptid=?");
			params.add(ptid);
		}		
		if(StringUtils.isNotBlank(mobile)){
			sql.append(" and u.mobile like ?");
			params.add("%"+mobile+"%");
		}
/*			if(StringUtils.isNotBlank(terminal)){
				sql.append(" and terminal=?");
				params.add(terminal);
		}*/
		if(StringUtils.isNotBlank(leftCreateTime)&&StringUtils.isNotBlank(rightCreateTime)){
			sql.append(" and l.createtime between '"+leftCreateTime+"' and '"+rightCreateTime+"'");
		}
		if(StringUtils.isNotBlank(leftPayTime)&&StringUtils.isNotBlank(rightPayTime)){
			sql.append(" and l.paytime between '"+leftPayTime+"' and '"+rightPayTime+"'");
		}
		sql.append(" order by l.createtime desc");
		if(limit){
			sql.append(" limit ?,?");
			params.add((page-1)*rows);
			params.add(rows);			
		}
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<OrderInfo>();
		}
	}
	
	public List<OrderInfo> getRelationOrder(String createtime){
		try {
			return super.query("select v.* from (select * from orderinfo where createtime <? and (status = 0 or status = 1)) v left join userrelationrecord r on v.oid = r.oid where r.urrid is null",createtime);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<OrderInfo>();
		}
	}
}
